package com.itbupt.eat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbupt.eat.common.R;
import com.itbupt.eat.dto.DishDto;
import com.itbupt.eat.entity.Category;
import com.itbupt.eat.service.ICategoryService;
import com.itbupt.eat.service.IDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author BUPT-Dingchang
 * @Create 2024/4/29 19:15
 * ClassName: CategoryController
 * Description:
 */

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IDishService dishService;


    /**
     * 添加分类
     * @param category  分类信息
     * @return  R
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        if(categoryService.save(category)) {
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }


    /**
     * 分页查询
     * @param page  当前页
     * @param pageSize  每页显示条数
     * @return  R
     */
    @GetMapping("/page")
    public R<Page<Category>> getPage(Long page, Long pageSize) {
        return R.success(categoryService.getPage(page, pageSize));
    }


    /**
     * 删除分类
     * @param ids 分类id
     * @return R
     */
    @DeleteMapping
    public R<String> deleteById(Long ids) {
        if(categoryService.removeById(ids)) {
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }


    /**
     * 修改分类
     * @param category 分类信息
     * @return R
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        if (categoryService.updateById(category)) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


    /**
     * 添加菜品或者套餐时获取的分类信息
     * @param category 分类信息
     * @return R
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(category.getType() != null, Category::getType, category.getType());

        // 双重排序条件
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);

        return R.success(categoryService.list(wrapper));
    }



}
